$(function () {

    $("body").on("click","#save-again",function () {
        saveAgentUser(true);
    }).on("click","#save-back",function () {
        saveAgentUser(false);
    }).on('click','#agentUserName', function () {
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>选择一级推广员</strong>',
            area: winArea,
            content: baseloaction + '/admin/agent/user/selectAgentUserList',
            btn: ['选择', '取消'],
            yes: function (index, layero) {
                var selected = parent.layer.getChildFrame('body', index).find('input.i-checks:checked');
                if(selected.length<=0){
                    icModale(0, "请选择一级推广员");
                    return;
                }
                var em = selected[0];
                var agentUserName = em.title;
                var agentUserId = em.value;
                var settType = $(em).attr("settType");
                var twoScale = $(em).attr("twoScale");
                $("#parentId").val(agentUserId);
                $("#agentUserName").val(agentUserName);
                parent.layer.close(index);
                if(settType!='undefined' && settType != 0){
                    $("#twoscale").show();
                    $("#twoscale").find("input").val(twoScale);
                }
            }
        });
    }).on('blur',"input[name='oneScale'],input[name='twoScale'],input[name='bonusScale']",function(){//比例输入监听
        var value = $(this).val();
        if(!/^(\d|[1-9]\d)$/.test(value)){
            $(this).val(0);
        }
    }).on('blur',"input[name='mobile']",function(){//手机号输入监听
        var value = $(this).val();
        if(!/[1][1-9]\d{9}/.test(value)){
            $(this).val("");
            $(this).next('span').html("<span style='color:red' class='c-999'>输入格式不正确</span>");
        }else{
            $(this).next('span').html("<span class='help-block m-b-none c-999'>推广员手机号码</span>");
        }
    });

    function saveAgentUser(flag) {
        var params = $("#agentUser-form").serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/admin/agent/user/saveOrUpdateAgentUser',
            data: params,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (result) {
                icModale(0, result.message);
                if(result.success){
                    if(flag){
                        window.location.href = baseloaction + '/admin/agent/user/initAdd1?userType='+ $("#userType").val();
                    }else {
                        window.location.href = baseloaction + '/admin/agent/user/queryAgentUserList';
                    }
                }
            },
            error: function (error) {
                icModale(0, "系统繁忙，请稍后重试");
            }
        })
    }
});